<?php
class Tourism_goods_attr_model extends CI_Model
{
    private $table = 'tourism_goods_attr';
    private $table_1 = 'tourism_goods_base';
    private $table_3 = 'user';
    private $table_4 = 'tourism_cart_goods';
    
    public function findById($id)
    {
        $this->db->select('goods_attr_id,goods_num,attribute_value');
        $this->db->from($this->table);
        $this->db->where('goods_attr_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    public function findByGoodsInfo($id)
    {
         $this->db->select('tourism_goods_base.limit_num,tourism_goods_base.limit_day, transport_cost, tourism_goods_base.rate_id, tourism_goods_attr.is_check,weight,tourism_goods_attr.goods_num AS number,tourism_goods_attr.goods_attr_id,tourism_goods_attr.swim,tourism_goods_attr.market_price,tourism_goods_attr.supply_price,tourism_goods_attr.attribute_value,tourism_goods_attr.shop_price,
                    user.uid,user_name,user.alias_name,user.mobile_phone,tourism_goods_base.goods_name,tourism_goods_base.goods_id,tourism_goods_base.transport_id,tourism_goods_base.images');
        $this->db->from($this->table.' as tourism_goods_attr');
        $this->db->join($this->table_1.' as tourism_goods_base','tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->join($this->table_3.' as user','tourism_goods_base.uid = user.uid');
//        $this->db->where('tourism_goods_base.is_on_sale',1);
        $this->db->where('tourism_goods_attr.goods_attr_id',$id);
        $this->db->limit(1);
        return $this->db->get();
    }

    public function findByVaildateNum($ids, $number)
    {
        $this->db->select('tourism_goods_attr.goods_attr_id,tourism_goods_attr.attribute_value,tourism_goods_attr.shop_price,tourism_goods_attr.market_price,tourism_goods_attr.supply_price,
                    user.uid,user.user_name,user.alias_name,tourism_goods_base.goods_name,tourism_goods_base.goods_id,tourism_goods_base.transport_id,tourism_goods_base.images');
        $this->db->from($this->table.' as tourism_goods_attr');
        $this->db->join($this->table_1.' as tourism_goods_base','tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->join($this->table_3.' as user','tourism_goods_base.uid = user.uid');
        $this->db->where('tourism_goods_base.is_on_sale',1);
        $sql = '(';
        foreach($ids as $key=>$id){
            if($key > 0){
                $sql .= ' OR ';
            }
            $sql .= '(`tourism_goods_attr`.`goods_attr_id`='.$id.' AND `tourism_goods_attr`.`goods_num` >= '.$number[$key].')';
        }
        $this->db->where($sql.')');
        $this->db->group_by('`tourism_goods_attr`.`goods_attr_id`');
        return $this->db->get();
    }

    public function findByCartGoods($id,$uid)
    {
        $this->db->select('tourism_goods_attr.weight,tourism_goods_attr.goods_num as number,tourism_goods_attr.attribute_value,tourism_goods_attr.goods_attr_id,id,tourism_cart_goods.goods_num');
        $this->db->from($this->table.' AS tourism_goods_attr');
        $this->db->join($this->table_4.' AS tourism_cart_goods', "tourism_cart_goods.goods_attr_id=tourism_goods_attr.goods_attr_id AND tourism_cart_goods.uid={$uid} ", 'left');
        $this->db->where('tourism_goods_attr.goods_attr_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

    /*
   * 商品修改 创建订单时候使用变更销量
   * */
    public function updateGoodsNum($param)
    {
        $this->db->where('goods_attr_id', $param['goods_attr_id']);
        $this->db->set('goods_num', 'goods_num+' . $param['goods_num'], FALSE);
        return $this->db->update($this->table);
    }

    /*
 * 商品修改 创建订单时候使用变更销量
 * */
    public function updateTourismSale($param)
    {
        $this->db->where('goods_attr_id', $param['goods_attr_id']);
        $this->db->set('sale_count', 'sale_count+' . $param['number'], FALSE);
        return $this->db->update($this->table);
    }

    public function findByAttrId($id)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('goods_attr_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

    public function findGoodsBase($id)
    {
        $this->db->select('tourism_goods_attr.*');
        $this->db->from($this->table.' AS tourism_goods_attr');
        $this->db->join($this->table_1.' AS tourism_goods_base', 'tourism_goods_base.goods_id = tourism_goods_attr.goods_id', 'inner');
        $this->db->where('tourism_goods_attr.goods_attr_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    /**
     * 更新浏览量
     * @param unknown $param
     */
    public function setTourismCount($id)
    {
        $this->db->where('goods_attr_id',$id);
        $this->db->set('tour_count', 'tour_count+1', FALSE);
        return $this->db->update($this->table);
    }
}